package com.winit.openapi.gfs.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.gfs.spi.EbayUploadTrackingNoConfigService;
import com.winit.gfs.spi.command.AddEbayUploadConfigCommand;
import com.winit.gfs.spi.command.GetEbayUploadConfigCommand;
import com.winit.gfs.spi.command.UpdateEbayUploadConfigCommand;
import com.winit.gfs.spi.vo.EbayUploadTrackingNoConfigVo;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;

/**
 * 
 * ebay上传跟踪号配置Controller
 * 
 * @version 
 * <pre>
 * Author	Version		Date		Changes
 * kang.wang 	1.0  		2015年11月12日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value="/gfs/uploadConfig")
public class EbayUploadTrackingNoConfigController extends BaseController {

    @Resource
    private EbayUploadTrackingNoConfigService ebayUploadTrackingNoConfigService;
    
    @RequestMapping(value = "/query")
    public String queryEbayUploadConfig(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "name");
            ValidateUtil.validMaxLength(json, "name", 50);
            ValidateUtil.validNotNull(json, "category");
            ValidateUtil.validMaxLength(json, "category", 32);
            
            GetEbayUploadConfigCommand command = new GetEbayUploadConfigCommand();
            EbayUploadTrackingNoConfigVo vo = new EbayUploadTrackingNoConfigVo();
            vo.setName(json.getString("name"));
            vo.setCategory(json.getString("category"));
            CommandContext ctx = CommandContext.getContext();
            vo.setBpartnerId(Long.valueOf(ctx.getUserId()));
            String platform = null;
            if(json.containsKey("platform")){
                platform = json.getString("platform");
            }else{
                platform = "ebay";
            }
            vo.setPlatform(platform);
            command.setVo(vo);
            command.setCtx(ctx);
            
            // 查询配置
            EbayUploadTrackingNoConfigVo rsVo = ebayUploadTrackingNoConfigService.getUploadConfigByName(command);
            Map<String,Object> data = new HashMap<String,Object>();
            if(rsVo != null){
                data.put("name", rsVo.getName());
                data.put("category", rsVo.getCategory());
                data.put("value", rsVo.getValue());
                data.put("platform", rsVo.getPlatform());
            }else{
                // 不存在返回默认值
                data.put("name", json.getString("name"));
                data.put("category", json.getString("category"));
                data.put("value", ApiConstant.EBAY_UPLOAD_TRACKINGNO_N);
                data.put("platform", platform);
            }
            
            responseMsg.setData(data);
        }else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        
        return SUCCESS;
    }
    
    @RequestMapping(value = "/update")
    public String updateEbayUploadConfig(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "name");
            ValidateUtil.validMaxLength(json, "name", 50);
            ValidateUtil.validNotNull(json, "value");
            ValidateUtil.validMaxLength(json, "value", 50);
            ValidateUtil.validNotNull(json, "category");
            ValidateUtil.validMaxLength(json, "category", 32);
            
            GetEbayUploadConfigCommand command = new GetEbayUploadConfigCommand();
            EbayUploadTrackingNoConfigVo vo = new EbayUploadTrackingNoConfigVo();
            vo.setName(json.getString("name"));
            vo.setCategory(json.getString("category"));
            vo.setValue(json.getString("value"));
            CommandContext ctx = CommandContext.getContext();
            vo.setBpartnerId(Long.valueOf(ctx.getUserId()));
            String platform = null;
            if(json.containsKey("platform")){
                platform = json.getString("platform");
            }else{
                platform = "ebay";
            }
            vo.setPlatform(platform);
            command.setVo(vo);
            command.setCtx(ctx);
            
            // 查询配置
            EbayUploadTrackingNoConfigVo rsVo = ebayUploadTrackingNoConfigService.getUploadConfigByName(command);
            
            // 存在配置就更新
            if(rsVo != null){
                UpdateEbayUploadConfigCommand updateCommand = new UpdateEbayUploadConfigCommand();
                updateCommand.setCtx(ctx);
                EbayUploadTrackingNoConfigVo updateVo = new EbayUploadTrackingNoConfigVo();
                updateVo.setId(rsVo.getId());
                updateVo.setValue(json.getString("value"));
                updateCommand.setVo(updateVo);
                updateVo.setPlatform(platform);
                
                ebayUploadTrackingNoConfigService.updateUploadConfigById(updateCommand);
            }else {
                // 不存在配置就添加一个
                AddEbayUploadConfigCommand addCommand = new AddEbayUploadConfigCommand();
                addCommand.setCtx(ctx);
                EbayUploadTrackingNoConfigVo addVo = new EbayUploadTrackingNoConfigVo();
                addVo.setName(json.getString("name"));
                addVo.setCategory(json.getString("category"));
                addVo.setValue(json.getString("value"));
                addVo.setBpartnerId(Long.valueOf(ctx.getUserId()));
                addVo.setOrganizationId(Long.valueOf(ctx.getOrgId()));
                addVo.setCreatedby(ctx.getUserId());
                addVo.setUpdatedby(ctx.getUserId());
                addVo.setPlatform(platform);
                addCommand.setVo(addVo);
                
                ebayUploadTrackingNoConfigService.addUploadConfig(addCommand);
            }
            
        }else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        return SUCCESS;
    }
}
